<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑新闻 - 百度新闻平台</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        /* 全局样式 */
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background-color: #f8f9fa;
        }
        
        /* 顶部导航样式 */
        .top-navbar {
            background-color: #ffffff;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            padding: 10px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .logo {
            font-size: 20px;
            font-weight: 600;
            color: #007bff;
        }
        
        .logout-btn {
            color: #666;
            text-decoration: none;
            transition: color 0.3s ease;
        }
        
        .logout-btn:hover {
            color: #007bff;
        }
        
        /* 主容器样式 */
        .container {
            margin-top: 30px;
        }
        
        /* 页面标题样式 */
        .page-title {
            font-size: 24px;
            font-weight: 600;
            color: #333;
            margin-bottom: 30px;
        }
        
        /* 表单容器样式 */
        .form-container {
            background-color: #ffffff;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            padding: 30px;
        }
        
        /* 表单样式 */
        .form-group {
            margin-bottom: 25px;
        }
        
        .form-group label {
            display: block;
            font-weight: 600;
            color: #333;
            margin-bottom: 8px;
        }
        
        .form-control {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            transition: border-color 0.3s ease;
        }
        
        .form-control:focus {
            outline: none;
            border-color: #007bff;
            box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
        }
        
        /* 文本区域样式 */
        textarea.form-control {
            resize: vertical;
            min-height: 150px;
        }
        
        /* 按钮样式 */
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: background-color 0.3s ease;
            text-decoration: none;
            display: inline-block;
            margin-right: 10px;
        }
        
        .btn-primary {
            background-color: #007bff;
            color: white;
        }
        
        .btn-primary:hover {
            background-color: #0056b3;
        }
        
        .btn-secondary {
            background-color: #6c757d;
            color: white;
        }
        
        .btn-secondary:hover {
            background-color: #5a6268;
        }
        
        /* 按钮组样式 */
        .btn-group {
            margin-top: 30px;
        }
        
        /* 返回按钮样式 */
        .back-link {
            display: inline-block;
            margin-top: 20px;
            color: #007bff;
            text-decoration: none;
            transition: color 0.3s ease;
        }
        
        .back-link:hover {
            color: #0056b3;
        }
        
        /* 图片上传预览样式 */
        .image-upload {
            border: 2px dashed #ddd;
            border-radius: 4px;
            padding: 20px;
            text-align: center;
            cursor: pointer;
            transition: border-color 0.3s ease;
            margin-bottom: 10px;
        }
        
        .image-upload:hover {
            border-color: #007bff;
        }
        
        .image-preview {
            max-width: 100%;
            max-height: 200px;
            margin-top: 10px;
            display: none;
        }
        
        /* 提示信息样式 */
        .help-text {
            font-size: 12px;
            color: #999;
            margin-top: 5px;
        }
        
        /* 响应式调整 */
        @media (max-width: 768px) {
            .top-navbar {
                padding: 10px;
            }
            
            .container {
                margin-top: 20px;
            }
            
            .form-container {
                padding: 20px;
            }
            
            .page-title {
                font-size: 20px;
            }
        }
    </style>
</head>
<body>
    <!-- 顶部导航 -->
    <div class="top-navbar">
        <div class="logo">业天智能新闻 - 管理系统</div>
        <div>
            <a href="/admin/logout" class="logout-btn">退出登录</a>
        </div>
    </div>

    <!-- 主要内容 -->
    <div class="container">
        <!-- 页面标题 -->
        <h1 class="page-title">编辑新闻</h1>
        
        <!-- 表单容器 -->
        <div class="form-container">
            <form method="POST" action="/admin/news/edit/{{ news.id }}" enctype="multipart/form-data">
                <!-- 新闻标题 -->
                <div class="form-group">
                    <label for="title">新闻标题 <span style="color: red;">*</span></label>
                    <input type="text" class="form-control" id="title" name="title" value="{{ news.title }}" placeholder="请输入新闻标题" required>
                </div>
                
                <!-- 封面图片 -->
                <div class="form-group">
                    <label for="face_img">封面图片</label>
                    <div class="image-upload">
                        <input type="file" id="face_img" name="face_img" style="display: none;">
                        <span>点击上传封面图片</span>
                        <div class="help-text">支持JPG、PNG格式，建议尺寸：800x450像素</div>
                    </div>
                    <img id="image_preview" class="image-preview" src="" alt="封面预览">
                </div>
                
                <!-- 新闻内容 -->
                <div class="form-group">
                    <label for="content">新闻内容</label>
                    <textarea class="form-control" id="content" name="content" placeholder="请输入新闻内容"></textarea>
                    <div class="help-text">支持富文本编辑，可输入图文内容</div>
                </div>
                
                <!-- 排序号 -->
                <!-- div class="form-group">
                    <label for="order_num">排序号</label>
                    <input type="number" class="form-control" id="order_num" name="order_num" min="0" value="0" placeholder="请输入排序号">
                    <div class="help-text">数字越大，排序越靠前</div>
                </div -->
                
                <!-- 发布状态 -->
                <!-- div class="form-group">
                    <label for="status">发布状态</label>
                    <select class="form-control" id="status" name="status">
                        <option value="1">已发布</option>
                        <option value="0">未发布</option>
                    </select>
                </div -->
                
                <!-- 按钮组 -->
                <div class="btn-group">
                    <button type="submit" class="btn btn-primary">保存修改</button>
                    <a href="/admin" class="btn btn-secondary">取消</a>
                </div>
            </form>
            
            <!-- 返回按钮 -->
            <a href="/admin" class="back-link">← 返回新闻列表</a>
        </div>
    </div>

    <!-- JavaScript -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="/static/js/rich_text_editor.js"></script>
    <script>
        // 页面加载完成后执行
        $(document).ready(function() {
            // 点击上传图片区域时，触发文件选择框
            $('.image-upload').on('click', function(e) {
                e.preventDefault();
                e.stopPropagation();
                $('#face_img').trigger('click');
            });
            
            // 确保点击span元素也能触发文件选择框
            $('.image-upload span').on('click', function(e) {
                e.preventDefault();
                e.stopPropagation();
                $('#face_img').trigger('click');
            });
            
            // 阻止默认点击行为
            $('#face_img').on('click', function(e) {
                e.stopPropagation();
            });
            
            // 选择文件后，预览图片
            $('#face_img').change(function() {
                if (this.files && this.files[0]) {
                    const reader = new FileReader();
                    
                    reader.onload = function(e) {
                        $('#image_preview').attr('src', e.target.result);
                        $('#image_preview').show();
                    }
                    
                    reader.readAsDataURL(this.files[0]);
                }
            });
            
            
            // 为表单添加提交前的验证
            $('form').submit(function() {
                // 验证标题不能为空
                const title = $('#title').val().trim();
                if (!title) {
                    alert('请输入新闻标题');
                    $('#title').focus();
                    return false;
                }
                
                // 显示保存中的提示
                alert('正在保存修改，请稍候...');
                
                return true;
            });
            
            // 如果有封面图片，则显示预览
            const faceImg = "{{ news.face_img | default('', true) }}";
            if (faceImg) {
                $('#image_preview').attr('src', faceImg);
                $('#image_preview').show();
                $('.face_img').val( faceImg);
            }

            // 初始化富文本编辑器 - 使用tojson过滤器解决单引号冲突问题
            const initialContent = {{ news.content | default("", true) | tojson }};
            const editor = initRichTextEditor('content', initialContent);
            
            
            // 设置当前发布状态
            //const currentStatus = '{{ news.status | default(1) }}';
            //$('#status').val(currentStatus);
            
            // 设置当前排序号
            //const currentOrderNum = '{{ news.order_num | default(0) }}';
            //$('#order_num').val(currentOrderNum);
        });
    </script>
</body>
</html>